//
//  AlertTool.m
//  liveFrame
//
//  Created by apple on 16/8/4.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AlertTool.h"
#import "MBProgressHUD.h"

@interface AlertTool ()

@end
static MBProgressHUD  *hud;
@implementation AlertTool

+(void)showInView:(UIView *)view{
    [self showInView:view withTitle:nil];
}
+(void)showInView:(UIView *)view withTitle:(NSString *)title{
    [self ShowInView:view withMode:MBProgressHUDModeIndeterminate withTitle:title];
}

+(void)showInView:(UIView *_Nonnull)view onlyWithTitle:(NSString *_Nonnull)title {
    [self showInView:view onlyWithTitle:title autoDisapper:YES];
}

+(void)showInView:(UIView *_Nonnull)view onlyWithTitle:(NSString * _Nullable)title autoDisapper:(BOOL)isAuto{
    [self ShowInView:view withMode:MBProgressHUDModeText withTitle:title];
    if (isAuto) {
        [self hiddenAfterDelyte:1.0];
    }
}

+(void)showErrorInView:(UIView *)view withTitle:(NSString *)title{
    [self showInView:view onlyWithTitle:title autoDisapper:YES];
}

+(void)showWithCustomModeInView:(UIView *)view {
    [self showWithCustomModeInView:view withTitle:nil];
}
+(void)showWithCustomModeInView:(UIView *)view withTitle:(NSString *)title{
    [self ShowInView:view withMode:MBProgressHUDModeCustomView withTitle:title];
}

+(void)ShowInView:(UIView *)view withMode:(MBProgressHUDMode)hudMode withTitle:(NSString *)title
{
    [hud removeFromSuperview];
    hud = nil;
    if (!view) return;
    hud = [[MBProgressHUD alloc]initWithView:view];
    [view addSubview:hud];
    [view bringSubviewToFront:hud];
    hud.mode = hudMode;
    if (hudMode == MBProgressHUDModeCustomView) {
        NSMutableArray *arr = [NSMutableArray array];
        for (NSInteger i =1; i<3; i++) {
            NSString *imageString = [NSString stringWithFormat:@"gif%lu",i];
            [arr addObject:[UIImage imageNamed:imageString]];
        }
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
        imageView.animationImages  = arr;
        imageView.animationDuration = 1.0;
        imageView.animationRepeatCount = 0;
        [imageView startAnimating];
        hud.customView = imageView;
        hud.color = [UIColor clearColor];
    }else{
        hud.customView = nil;
    }
    hud.detailsLabelText = title;
    hud.detailsLabelFont = [UIFont systemFontOfSize:14];
    hud.removeFromSuperViewOnHide = YES;
    [hud show:YES];
}

+(void)hidden{
    [self hiddenAfterDelyte:0.0];
}
+(void)hiddenAfterDelyte:(NSTimeInterval)delyte
{
    [hud hide:YES afterDelay:delyte];
    hud = nil;
}

+(void)alertStyleWithTitle:(NSString *)title message:(NSString *)message needCancelCallBack:(BOOL)needCancelCallBack cancelButtonTitle:(NSString *)cancelTitle destructiveButtonTitle:(NSString *)destructiveTitle presentingController:(UIViewController *)presentingController otherButtonTitles:(NSArray *)otherButtonTitles callbackBlock:(JLXXAlertToolBlockWithoutTextFiled)block{
    
    [self alertControllerStyle:UIAlertControllerStyleAlert withTitle:title message:message needCancelCallBack:needCancelCallBack cancelButtonTitle:cancelTitle destructiveButtonTitle:destructiveTitle presentingController:presentingController otherButtonTitles:otherButtonTitles callbackBlock:block];

}

+(void)alertStyleWithTitle:(NSString *)title message:(NSString *)message needCancelCallBack:(BOOL)needCancelCallBack cancelButtonTitle:(NSString *)cancelTitle destructiveButtonTitle:(NSString *)destructiveTitle textFiledPlaceholder:(NSString *)placeholder textFieldKeyboardType:(UIKeyboardType)keyboardType presentingController:(UIViewController *)presentingController otherButtonTitles:(NSArray *)otherButtonTitles callbackBlock:(JLXXAlertToolBlock)block{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
   
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.keyboardType = keyboardType;
        textField.placeholder = placeholder;
    }];
    __weak typeof(alertController) weakAlertController= alertController;
    //添加取消按钮,取消按钮不需要回调事件
    if (cancelTitle) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
            if (needCancelCallBack) {
                block(0,weakAlertController.textFields.firstObject);
            }
        }];
        [alertController addAction:cancelAction];
    }
    if (destructiveTitle) {
        UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            if (cancelTitle) {
                block(1,weakAlertController.textFields.firstObject);
            }else {
                block(0,weakAlertController.textFields.firstObject);
            }
        }];
        [alertController addAction:destructiveAction];
    }
    if (otherButtonTitles.count > 0)
    {
        int count;
        if (!cancelTitle && !destructiveTitle) {
            count = 0;
        }
        else if ((cancelTitle && !destructiveTitle) || (!cancelTitle && destructiveTitle)) {
            count = 1;
        }
        else if (cancelTitle && destructiveTitle) {
            count = 2;
        }
        
        for (NSString *title in otherButtonTitles) {
            UIAlertAction *otherAction = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                block(count,weakAlertController.textFields.firstObject);
            }];
            [alertController addAction:otherAction];
            count ++;
        }
    }
    [presentingController presentViewController:alertController animated:YES completion:nil];
}

+(void)sheetStyleWithTitle:(NSString *)title message:(NSString *)message needCancelCallBack:(BOOL)needCancelCallBack cancelButtonTitle:(NSString *)cancelTitle destructiveButtonTitle:(NSString *)destructiveTitle presentingController:(UIViewController *)presentingController otherButtonTitles:(NSArray *)otherButtonTitles callbackBlock:(JLXXAlertToolBlockWithoutTextFiled)block{
   
    [self alertControllerStyle:UIAlertControllerStyleActionSheet withTitle:title message:message needCancelCallBack:needCancelCallBack cancelButtonTitle:cancelTitle destructiveButtonTitle:destructiveTitle presentingController:presentingController otherButtonTitles:otherButtonTitles callbackBlock:block];
}

+(void)alertControllerStyle:(UIAlertControllerStyle)style withTitle:(NSString *)title message:(NSString *)message needCancelCallBack:(BOOL)needCancelCallBack cancelButtonTitle:(NSString *)cancelTitle destructiveButtonTitle:(NSString *)destructiveTitle presentingController:(UIViewController *)presentingController otherButtonTitles:(NSArray *)otherButtonTitles callbackBlock:(JLXXAlertToolBlockWithoutTextFiled)block{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:style];
    
    if (cancelTitle) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            //添加取消按钮,取消按钮需要回调事件
            if (needCancelCallBack) {
                block(0);
            }
        }];
        [alertController addAction:cancelAction];
    }
    if (destructiveTitle) {
        UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            if (cancelTitle) {
                block(1);
            }else {
                block(0);
            }
        }];
        [alertController addAction:destructiveAction];
    }
    if (otherButtonTitles.count > 0)
    {
        int count;
        if (!cancelTitle && !destructiveTitle) {
            count = 0;
        }
        else if ((cancelTitle && !destructiveTitle) || (!cancelTitle && destructiveTitle)) {
            count = 1;
        }
        else if (cancelTitle && destructiveTitle) {
            count = 2;
        }
        
        for (NSString *title in otherButtonTitles) {
            UIAlertAction *otherAction = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                block(count);
            }];
            [alertController addAction:otherAction];
            count ++;
        }
    }
    [presentingController presentViewController:alertController animated:YES completion:nil];
}
@end
